package com.ivan.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.common.result.Result;
import com.ivan.model.entity.hosp.HospitalSet;
import com.ivan.hospital.service.HospitalSetService;
import com.ivan.model.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ivan
 * @date 2021/3/13 18:48
 * @Description:
 */
@Api(tags = "医院设置管理接口")
//@CrossOrigin//使用gateway后做了配置
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * @author Ivan
     * @date 2021/3/13 18:52
     * @Description: 查询所有医院设置数据
     */
    @ApiOperation(value = "查询所有医院设置数据")
    @GetMapping("/getAll")
    public Result getAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * @author Ivan
     * @date 2021/3/13 21:17
     * @Description: 根据id逻辑删除医院设置数据
     */
    @ApiOperation(value = "根据id删除医院设置数据")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2021/3/13 21:59
     * @Description: 条件查询带分页
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") long current,
                             @PathVariable("limit") long limit,
                             @RequestBody(required = false) HospitalSetQueryVo queryVo) {
        Page<HospitalSet> hospitalSetPage = hospitalSetService.findByPage(current, limit, queryVo);
        return Result.ok(hospitalSetPage);
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:19
     * @Description: 添加医院设置
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        int save = hospitalSetService.saveHospitalSet(hospitalSet);
        if (save > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:32
     * @Description: 根据id查询医院设置数据
     */
    @ApiOperation(value = "根据id查询医院设置数据")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:32
     * @Description: 修改医院设置数据
     */
    @ApiOperation(value = "修改医院设置数据")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:34
     * @Description: 批量删除医院设置数据
     */
    @ApiOperation(value = "批量删除医院设置数据")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> idList) {
        boolean delete = hospitalSetService.removeByIds(idList);
        if (delete) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:41
     * @Description: 锁定/解锁医院设置
     */
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status) {
        int update = hospitalSetService.lockHospitalSet(id, status);
        if (update > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2021/3/13 22:46
     * @Description: 发送医院设置密钥
     */
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id) {
        //根据id查询医院设置数据
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信--后续完善
        return Result.ok();
    }
}
